home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_xemacs.idb / usr / freeware / lib / xemacs-20.4 / lisp / prim / macros.el.z / macros.el
Encoding:
Text File  |  1998-05-21  |  7.7 KB  |  227 lines

  1. ;;; macros.el --- non-primitive commands for keyboard macros.
  2.  
  3. ;; Copyright (C) 1985, 86, 87, 92, 94, 95 Free Software Foundation, Inc.
  4.  
  5. ;; Maintainer: FSF
  6. ;; Keywords: abbrev
  7.  
  8. ;; This file is part of XEmacs.
  9.  
  10. ;; XEmacs is free software; you can redistribute it and/or modify it
  11. ;; under the terms of the GNU General Public License as published by
  12. ;; the Free Software Foundation; either version 2, or (at your option)
  13. ;; any later version.
  14.  
  15. ;; XEmacs is distributed in the hope that it will be useful, but
  16. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  17. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  18. ;; General Public License for more details.
  19.  
  20. ;; You should have received a copy of the GNU General Public License
  21. ;; along with XEmacs; see the file COPYING.  If not, write to the 
  22. ;; Free Software Foundation, 59 Temple Place - Suite 330,
  23. ;; Boston, MA 02111-1307, USA.
  24.  
  25. ;;; Synched up with: FSF 19.34.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; Extension commands for keyboard macros.  These permit you to assign
  30. ;; a name to the last-defined keyboard macro, expand and insert the
  31. ;; lisp corresponding to a macro, query the user from within a macro,
  32. ;; or apply a macro to each line in the reason.
  33.  
  34. ;; This file is largely superseded by edmacro.el as of XEmacs 20.1. -sb
  35.  
  36. ;;; Code:
  37.  
  38. ;;;###autoload
  39. (defun name-last-kbd-macro (symbol)
  40.   "Assign a name to the last keyboard macro defined.
  41. Argument SYMBOL is the name to define.
  42. The symbol's function definition becomes the keyboard macro string.
  43. Such a \"function\" cannot be called from Lisp, but it is a valid
  44. editor command."
  45.   (interactive "SName for last kbd macro: ")
  46.   (or last-kbd-macro
  47.       (error "No keyboard macro defined"))
  48.   (and (fboundp symbol)
  49.        (not (stringp (symbol-function symbol)))
  50.        (not (vectorp (symbol-function symbol)))
  51.        (error "Function %s is already defined and not a keyboard macro."
  52.           symbol))
  53.   (if (string-equal symbol "")
  54.       (error "No command name given"))
  55.   (fset symbol last-kbd-macro))
  56.  
  57. ;;; Moved here from edmacro.el:
  58.  
  59. ;;;###autoload
  60. (defun insert-kbd-macro (macroname &optional keys)
  61.   "Insert in buffer the definition of kbd macro NAME, as Lisp code.
  62. Optional second arg KEYS means also record the keys it is on
  63. \(this is the prefix argument, when calling interactively).
  64.  
  65. This Lisp code will, when executed, define the kbd macro with the same
  66. definition it has now.  If you say to record the keys, the Lisp code
  67. will also rebind those keys to the macro.  Only global key bindings
  68. are recorded since executing this Lisp code always makes global
  69. bindings.
  70.  
  71. To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
  72. use this command, and then save the file."
  73.   (interactive "CInsert kbd macro (name): \nP")
  74.   (let (definition)
  75.     (if (string= (symbol-name macroname) "")
  76.     (progn
  77.       (setq definition (format-kbd-macro))
  78.       (insert "(setq last-kbd-macro"))
  79.       (setq definition (format-kbd-macro macroname))
  80.       (insert (format "(defalias '%s" macroname)))
  81.     (if (> (length definition) 50)
  82.     (insert " (read-kbd-macro\n")
  83.       (insert "\n  (read-kbd-macro "))
  84.     (prin1 definition (current-buffer))
  85.     (insert "))\n")
  86.     (if keys
  87.     (let ((keys (where-is-internal macroname)))
  88.       (while keys
  89.         (insert (format "(global-set-key %S '%s)\n" (car keys) macroname))
  90.         (pop keys))))))
  91.  
  92. ;;;###autoload
  93. (defun kbd-macro-query (flag)
  94.   "Query user during kbd macro execution.
  95. With prefix argument, enters recursive edit,
  96.  reading keyboard commands even within a kbd macro.
  97.  You can give different commands each time the macro executes.
  98. Without prefix argument, asks whether to continue running the macro.
  99. Your options are: \\<query-replace-map>
  100. \\[act]    Finish this iteration normally and continue with the next.
  101. \\[skip]    Skip the rest of this iteration, and start the next.
  102. \\[exit]    Stop the macro entirely right now.
  103. \\[recenter]    Redisplay the frame, then ask again.
  104. \\[edit]    Enter recursive edit; ask again when you exit from that."
  105.   (interactive "P")
  106.   (or executing-kbd-macro
  107.       defining-kbd-macro
  108.       (error "Not defining or executing kbd macro"))
  109.   (if flag
  110.       (let (executing-kbd-macro defining-kbd-macro)
  111.     (recursive-edit))
  112.     (if (not executing-kbd-macro)
  113.     nil
  114.       (let ((loop t)
  115.         (msg (substitute-command-keys
  116.                   "Proceed with macro?\\<query-replace-map>\
  117.  (\\[act], \\[skip], \\[exit], \\[recenter], \\[edit]) ")))
  118.     (while loop
  119.       (let ((key (let ((executing-kbd-macro nil)
  120.                 (defining-kbd-macro nil))
  121.                        (message "%s" msg)
  122.                ;; XEmacs: avoid `read-char'.
  123.                        (read-char-exclusive)))
  124.                 def)
  125.         (setq key (vector key))
  126.         (setq def (lookup-key query-replace-map key))
  127.         (cond ((eq def 'act)
  128.            (setq loop nil))
  129.           ((eq def 'skip)
  130.            (setq loop nil)
  131.            (setq executing-kbd-macro ""))
  132.           ((eq def 'exit)
  133.            (setq loop nil)
  134.            (setq executing-kbd-macro t))
  135.           ((eq def 'recenter)
  136.            (recenter nil))
  137.           ((eq def 'edit)
  138.            (let (executing-kbd-macro defining-kbd-macro)
  139.              (recursive-edit)))
  140.           ((eq def 'quit)
  141.            (setq quit-flag t))
  142.           (t
  143.            (or (eq def 'help)
  144.                (ding))
  145.            (with-output-to-temp-buffer "*Help*"
  146.              (princ
  147.               (substitute-command-keys
  148.                "Specify how to proceed with keyboard macro execution.
  149. Possibilities: \\<query-replace-map>
  150. \\[act]    Finish this iteration normally and continue with the next.
  151. \\[skip]    Skip the rest of this iteration, and start the next.
  152. \\[exit]    Stop the macro entirely right now.
  153. \\[recenter]    Redisplay the frame, then ask again.
  154. \\[edit]    Enter recursive edit; ask again when you exit from that."))
  155.              (save-excursion
  156.                (set-buffer standard-output)
  157.                (help-mode)))))))))))
  158.  
  159. ;;;###autoload
  160. (defun apply-macro-to-region-lines (top bottom &optional macro)
  161.   "For each complete line between point and mark, move to the beginning
  162. of the line, and run the last keyboard macro.
  163.  
  164. When called from lisp, this function takes two arguments TOP and
  165. BOTTOM, describing the current region.  TOP must be before BOTTOM.
  166. The optional third argument MACRO specifies a keyboard macro to
  167. execute.
  168.  
  169. This is useful for quoting or unquoting included text, adding and
  170. removing comments, or producing tables where the entries are regular.
  171.  
  172. For example, in Usenet articles, sections of text quoted from another
  173. author are indented, or have each line start with `>'.  To quote a
  174. section of text, define a keyboard macro which inserts `>', put point
  175. and mark at opposite ends of the quoted section, and use
  176. `\\[apply-macro-to-region-lines]' to mark the entire section.
  177.  
  178. Suppose you wanted to build a keyword table in C where each entry
  179. looked like this:
  180.  
  181.     { \"foo\", foo_data, foo_function }, 
  182.     { \"bar\", bar_data, bar_function },
  183.     { \"baz\", baz_data, baz_function },
  184.  
  185. You could enter the names in this format:
  186.  
  187.     foo
  188.     bar
  189.     baz
  190.  
  191. and write a macro to massage a word into a table entry:
  192.  
  193.     \\C-x (
  194.        \\M-d { \"\\C-y\", \\C-y_data, \\C-y_function },
  195.     \\C-x )
  196.  
  197. and then select the region of un-tablified names and use
  198. `\\[apply-macro-to-region-lines]' to build the table from the names.
  199. "
  200.   (interactive "r")
  201.   (or macro
  202.       (progn
  203.     (if (null last-kbd-macro)
  204.         (error "No keyboard macro has been defined."))
  205.     (setq macro last-kbd-macro)))
  206.   (save-excursion
  207.     (let ((end-marker (progn
  208.             (goto-char bottom)
  209.             (beginning-of-line)
  210.             (point-marker)))
  211.       next-line-marker)
  212.       (goto-char top)
  213.       (if (not (bolp))
  214.       (forward-line 1))
  215.       (setq next-line-marker (point-marker))
  216.       (while (< next-line-marker end-marker)
  217.     (goto-char next-line-marker)
  218.     (save-excursion
  219.       (forward-line 1)
  220.       (set-marker next-line-marker (point)))
  221.     (save-excursion
  222.       (execute-kbd-macro (or macro last-kbd-macro))))
  223.       (set-marker end-marker nil)
  224.       (set-marker next-line-marker nil))))
  225.  
  226. ;;; macros.el ends here
  227.